/**
 * 
 */
package pATT.profilingAlgorithm;

import java.io.File;
import java.util.Vector;

import pATT.bNEdit.base.RedBayes;
import pATT.core.InstanceManager;
import pATT.core.Instances;
import pATT.core.Chooser.ImageFilter;
import pATT.logger.Message;
import pATT.profile.BayesNetProfileComponent;
import pATT.profile.Profile;
import pATT.profile.ProfileComponent;




/**
 * @author Ignacio
 *
 */
public class SAVERAlgorithm extends BatchAlgorithm {

	private Profile profile = null;
	private String NetBayesPath = "";
	private RedBayes net = null;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
	public Profile run(Instances input) {

		//levanta Datos-Ususarios-n.xml
		if(InstanceManager.eINSTANCE.getFileLoaded() == null){
			return null;
		}
		String userDataPath = InstanceManager.eINSTANCE.getFileLoaded().getAbsolutePath();	
		
		if(!userDataPath.endsWith("."+ImageFilter.SAVER_EXTENSION)){
			return null;
		}
		
		String pathNetBayes = obtenerPathNetBayes();
		if(pathNetBayes.equals("")){
			return null;
		}
		net = new RedBayes("");

		net.loadFromXml03File(pathNetBayes);

		String data = net.load(userDataPath);

		net.updateNet(data);

		BayesNetProfileComponent component = new BayesNetProfileComponent(net);

		Vector<ProfileComponent> components = new Vector<ProfileComponent>();
		components.addElement(component);

		profile = new Profile(components,input); 

		return profile;

	}

/**
 * Retorna el path completo de la red de bayes elegida, si se elige una erronea se devuelve "".
 * @return path
 */
	private String obtenerPathNetBayes() {
		if(!getNetBayesPath().equals("") && !new File(getNetBayesPath()).isFile()){
			//si la ruta es invalida tira un excepcion
			return "";
		}

		if(getNetBayesPath().equals("") && !new File(Message.PATH_NET_BAYES_DEFAULT).isFile()){
			return "";
		}
		File netBayes = null;

		if(getNetBayesPath().equals("")){
			netBayes = new File(Message.PATH_NET_BAYES_DEFAULT);
		}else{
			netBayes = new File(getNetBayesPath());
		}

		return netBayes.getAbsolutePath();
	}


	@Override
	public String toText() {
		return profile.toText().toString();
	}

	@Override
	public String basicAlgorithmOutput() {

		return new String();
	}

	@Override
	public boolean needArffFile() {
		return false;
	}


	public RedBayes getNet() {
		return net;
	}


	public String getNetBayesPath() {
		return NetBayesPath;
	}


	public void setNetBayesPath(String netBayesPath) {
		NetBayesPath = netBayesPath.trim();
	}


}
